const express = require("express");
const request = require('request');
const axios = require('axios');
const CryptoJS = require("crypto-js");

// 云市场分配的密钥Id
const secretId = "AKIDoK5g9EZH6J5c23b2zF4arUXdDIu944ZmQiHp";
// 云市场分配的密钥Key
const secretKey = "m57k9sk028d6Oo54892tllx1ojqh02kxf7hpxx7D";

// 获取天气信息数据
async function getWeather(data){
  const source = "market";
// 签名
  const datetime = (new Date()).toUTCString();
  const signStr = "x-date: " + datetime + "\n" + "x-source: " + source;
  const sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(signStr, secretKey));
  const auth = 'hmac id="' + secretId + '", algorithm="hmac-sha1", headers="x-date x-source", signature="' + sign + '"';
  const res = await axios.get('https://service-6drgk6su-1258850945.gz.apigw.tencentcs.com/release/lundear/weather1d', {
    headers: {
      "X-Source": source,
      "X-Date": datetime,
      "Authorization": auth,
    },
    params: {
      areaCode: "440100",
      need1hour: '1',
      need3hour: '1',
      needIndex: "1",
      needObserve: '1',
      needalarm: '1'
    }
  });
  return {
    code: 200,
    msg: '请求成功',
    data: res.data.data
  }
 
}


exports.getWeather = getWeather
